<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button type="primary" size="mini" @click="onPost" :disabled="!message">
      发布</van-button
    >
  </div>
</template>

<script>
import { addComment } from '@/api/comment'
export default {
  name: 'PostComment',
  components: {},
  props: {
    // 如果是发布文章评论，则传递文章ID
    // 如果是发布评论回复，则传递评论ID
    target: {
      type: [Number, String, Object],
      required: true,
    },
    // 如果是发布评论回复，则也需要传递文章ID
    articleId: {
      type: [Number, String, Object],
      default: null,
    },
  },
  data() {
    return {
      message: '',
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onPost() {
      // this.$toast.loading({ message: '发布中', forbidClick: true })
      const { data } = await addComment({
        target: this.target.toString(), // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id)content: this.message，// 评论的内容
        content: this.message,
        art_id: this.articleId ? this.articleId.toString() : null,
        // 文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数。
      })
      this.$emit('post-success', data.data.new_obj)
      this.$toast.success('发布成功')
    },
  },
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  padding: 14px;
  align-items: center;
  .post-field {
    background: #f5f7f9;
    margin-right: 15px;
  }
}
</style>
